  import {
    request,
    calcScrollViewHeight,
    WXshowToast,
    WXNavigateTo
} from '../../utils/index.js'
import { Api } from '../../config/api.js'

const { VIP, GIFT_CARD_ZONE, GIFT_CARD, GIFT_CARD_SHOP } = Api.goodListType
const appInst = getApp();


Page({
  data: {
    tabbar: {},
  isIphoneX:null,
    imgBaseUrl: appInst.globalData.imgBaseUrl,
    goodList: [],
    tabs: [
      {
        id: 0,
        iconName: 'allgood_icon',
        text: '全部商品',
        isActive: true
      },
      {
        id: 1,
        iconName: 'vipzone_icon',
        text: '会员专享',
        isActive: false
      },
      {
        id: 2,
        iconName: 'giftcard_icon',
        text: '礼品卡专区',
        isActive: false
      }
    ],
    subTabs: [
      {
        id: 0,
        text: '礼品卡商城',
        isActive:  true
      },
      {
        id: 1,
        text: '礼品卡',
        isActive:false
      }
      
    ],
    currentTabIndex: 0,
    currentSubTabIndex: 0,
    // 设计商品列表 scroll-view 占据剩余高度(px)
    scrollViewHeight: 0,
    giftCart:[],
  },
  onLoad: function (options) {
      // this.giftCart()
     appInst.editTabbar(); //调用tabbar
    const lastElIdArr = ['#headImg', '#tabbar']
    this._calcScrollViewHeight(lastElIdArr)
    this._getGoodList()
  },
  _calcScrollViewHeight(lastElIdArr) {
    calcScrollViewHeight(lastElIdArr, this, 28).then(res => {
      this.setData({ scrollViewHeight: res })
    })
  },

onReady: function () {
    this.setData({
        isIphoneX: appInst.globalData.systemInfo.model.search('iPhone X') != -1 ? true : false
    })
},
  
  handleChangeTab: function (e) {
    const currentTabIndex = e.detail.tabIndex

    if (this.data.currentTabIndex === currentTabIndex) return

    this.setData({
      tabs: this.data.tabs.map(tab => ({
        ...tab,
        isActive: tab.id === currentTabIndex
      })),
      currentTabIndex
    })

    // 获取数据
    let type
    let lastElIdArr = ['#headImg', '#tabbar', '#subtab']

    if (this.data.currentTabIndex === 0) {
      lastElIdArr = ['#headImg', '#tabbar']
      type = undefined
    } else if (this.data.currentTabIndex === 1) {
      lastElIdArr = ['#headImg', '#tabbar']
      type = VIP
    } else if (this.data.currentTabIndex === 2) {
      lastElIdArr = ['#headImg', '#tabbar', '#subtab']
      if (this.data.currentSubTabIndex === 0) {
        type = GIFT_CARD
      } else if (this.data.currentSubTabIndex === 1) {
        type = GIFT_CARD_SHOP
      }
    }

    this._calcScrollViewHeight(lastElIdArr)
    this._getGoodList(type)
  },
  handleChangeSubTab(e) {
    const subtabIndex = e.currentTarget.dataset.subtabIndex

    if (this.data.currentSubTabIndex === subtabIndex) return

    this.setData({
      subTabs: this.data.subTabs.map(subtab => ({
        ...subtab,
        isActive: subtab.id === subtabIndex
      })),
      currentSubTabIndex: subtabIndex
    })


    let type
    if (this.data.currentSubTabIndex === 0) {
      type = GIFT_CARD
    } else if (this.data.currentSubTabIndex === 1) {
      type = GIFT_CARD_SHOP
    }

    this._getGoodList(type)
  },

//   获取商品列表
 async  _getGoodList(type) {
    let tag
    if (type) {
      tag = { tag: type }
    }

    const {data}  = await   request({
           url: Api.getGoodList,
           method: 'POST',
            data:tag
     })
     if(data){
      // let goodList = data.data.data.filter(function(item){
      //    return item.tid != 6&&item.tid != 4&&item.tid != 5
      // })
      
     }
     this.setData({
      goodList:data.data.data
    })
      
  },


// 我的礼品卡
  //  async giftCart() {
  //      const {
  //          data
  //      } = await request({
  //          url: Api.giftCart,
  //          method: "POST"
  //      })
  //      this.setData({
  //          giftCart:data.data
  //      })
  //       //  console.log(this.data.giftCart)
  //  },
  
    //   商品兑换
    handleChange(e){
       let goodid = e.currentTarget.dataset.id
       let price  = e.currentTarget.dataset.pice
        WXNavigateTo(`/pages/giftcard_change/giftcard_change?goodid=${goodid}&price=${price}`)      
    }

})
